<?php
define('BASE_URL', '/hospital_department/');

use src\controller\DepartmentCRUD;
use src\controller\LoginController;
use src\controller\NoticeCRUD;
use src\models\Department;
use src\core\Database;

spl_autoload_register(function ($class_name) {
//    echo "class_name的路径为: $class_name\n";
//    echo '__DIR__的路径为: ' . __DIR__ . "\n";
    $file_paths = [
        __DIR__ . '/' . str_replace('\\', '/', $class_name) . '.php',
    ];
    foreach ($file_paths as $file) {
        if (file_exists($file)) {
            require_once $file;
        }
    }
});

// 检查登录状态
$checkLogin = new LoginController();
$checkLogin->checkLogin();

// 路由参数解析
$controller = $_GET['c'] ?? 'home';
$action = $_GET['a'] ?? 'index';

//控制器映射表
$controllers = [
    'department' => DepartmentCRUD::class,
    'login' => LoginController::class,
    'notice' => NoticeCRUD::class
];

//路由分发
if (isset($controllers[$controller])) {
    $controllerInstance = new $controllers[$controller];
    if (method_exists($controllerInstance, $action)) {
        //执行控制器方法
//        echo "Controller: $controller, Action: $action";
        call_user_func([$controllerInstance, $action]);
        exit;
    }
}

//默认显示主页视图
$db = new Database();
$department = new Department($db->getConnection());
$totalDepartments = $department->getTotalRows();

//包含视图文件
include __DIR__ . '/src/views/index.view.php';
?>
